package com.atguigu.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.atguigu.utils.JwtHelper;
import com.atguigu.utils.MD5Util;
import com.atguigu.utils.Result;
import com.atguigu.utils.ResultCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
* @author ASUS
* @description 针对表【news_user】的数据库操作Service实现
* @createDate 2025-11-03 14:39:24
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtHelper jwtHelper;


    //登录接口实现
    @Override
    public Result login(User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername,user.getUsername());
        User loginUser=userMapper.selectOne(lambdaQueryWrapper);

        if(loginUser==null)
        {
            return Result.build(null, ResultCodeEnum.USERNAME_ERROR);
        }

        //密码验证
        if(!StringUtils.isEmpty(user.getUserPwd())
                && MD5Util.encrypt(user.getUserPwd()).equals(loginUser.getUserPwd())
        )
        {
            //登录成功
            //根据用户id生成token
            String token=jwtHelper.createToken(Long.valueOf(loginUser.getUid()));

            //将token封装到result中返回给前端
            Map data=new HashMap();
            data.put("token",token);

            return Result.ok(data);



        }

        return Result.build(null,ResultCodeEnum.PASSWORD_ERROR);
    }



    /*
    1.token是否在有效期
    2.根据token获取用户id
    3.根据用户id查询用户信息
    4.去掉密码再封装result返回给前端
     */
    @Override
    public Result getUserInfo(String token) {
        //判断token是否在有效期 true表示过期了 false表示没有过期
        boolean expiration = jwtHelper.isExpiration(token);
        if(expiration)
        {
            //token过期了
            return Result.build(null,ResultCodeEnum.NOTLOGIN);
        }
        long userId=jwtHelper.getUserId(token).longValue();

        User user=userMapper.selectById(userId);
        user.setUserPwd("");

        Map data=new HashMap();
        data.put("loginUser",user);

        return Result.ok(data);
    }


    /*
    检查账号是否可用
    1.根据账号进行count查询
    2.count==0可用
    3.count>0不可用
     */
    @Override
    public Result checkUserName(String username) {
        LambdaQueryWrapper<User> queryWrapper
                =new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);


        Long count=userMapper.selectCount(queryWrapper);
        if(count==0)
        {
            return Result.ok(null);
        }

        return Result.build(null,ResultCodeEnum.USERNAME_USED);
    }




        /*
    注册业务实现
    1.检查账号是否已经被注册
    2.密码加密处理
    3.账号数据保存
    4.返回结果
     */

    @Override
    public Result regist(User user) {
        LambdaQueryWrapper<User> queryWrapper
                =new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        Long count=userMapper.selectCount(queryWrapper);
        if(count>0)
        {
            return Result.build(null,ResultCodeEnum.USERNAME_USED);
        }
        user.setUserPwd(MD5Util.encrypt(user.getUserPwd()));

        userMapper.insert(user);

        return Result.ok( null);
    }






}




